package com.atguigu.springbootmybatisplusrabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueConfig {
    //队列
    public static final String DELAYED_QUEUE_NAME="delayed.queue";
    //延迟交换机
    public static final String DELAYED_EXCHANGE_NAME="delayed.exchange";
    //绑定标识 routingkey
    public static final String DELAYED_ROUTING_KEY="delayed.routingkey";

    @Bean//创建队列
    public Queue delayedQueue(){
        return new Queue(DELAYED_QUEUE_NAME);
    }
    @Bean//创建延迟交换机
    public CustomExchange delayedExchange(){
        Map<String,Object> args = new HashMap<String,Object>();
        //1.交换机名称2.交换机类型3.是否需要持久化4.是否需要自动删除5.其他参数
        args.put("x-delayed-type","direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true,false,args);
    }
    @Bean//进行关系绑定
    public Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue")Queue delayedQueue,
                                       @Qualifier("delayedExchange") CustomExchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
